<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="手机号">
                  <el-input v-model="name" clearable style="width: 95%" placeholder="请输入..."
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="注册门店">
                  <el-select v-model="shopId" clearable style="width: 95%" popper-class="eloption" placeholder="请选择..."
                    filterable>
                    <el-option v-for="shop in shopList" :key="shop.id" :value="shop.id" :label="shop.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="是否异常">
                  <el-select v-model="isException" clearable style="width: 95%" popper-class="eloption"
                    placeholder="请选择..." filterable>
                    <el-option value="0" label="正常" />
                    <el-option value="1" label="异常" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <!-- <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
    </div> -->
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" class="tablebox" border ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          align="center">
          <vxe-column type="seq" title="序号" width="80"></vxe-column>
          <vxe-column field="storeName" title="注册门店" />
          <vxe-column field="name" title="会员名称">
            <template v-slot="{ row }">{{
              row.name ? row.name : row.phone
            }}</template>
          </vxe-column>
          <vxe-column field="phone" title="手机号" />
          <vxe-column field="tokenNum" :title="$tokenName + '数量'">
            <template #header="{ column }">
              <div style="display: flex;justify-content: center;">
                <span>{{ $tokenName }}数量</span>
                <span style="display: flex;flex-direction: column;">
                  <i style="cursor: pointer;" class="el-icon-caret-top iconCls"
                    :class="sort === '1' ? 'iconClsActived' : ''" @click="sortFn('1')"></i>
                  <i style="cursor: pointer;transform: translateY(-5px);" :class="sort === '0' ? 'iconClsActived' : ''"
                    class="el-icon-caret-bottom iconCls" @click="sortFn('0')"></i>
                </span>
              </div>
            </template>
          </vxe-column>
          <vxe-column field="exchangeCount" title="核销次数" />
          <vxe-column field="isException" title="是否异常">
            <template v-slot="{ row }">
              <el-tag v-if="row.isException == '0'" type="success">正常</el-tag>
              <el-tag v-if="row.isException == '1'" type="error">异常</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="status" title="状态">
            <template v-slot="{ row }">
              <el-tag v-if="row.status == '0'" type="success">启用</el-tag>
              <el-tag v-if="row.status == '1'" type="info">禁用</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="createTime" title="创建时间">
            <template v-slot="{ row }">
              {{ row.createTime | parseTime }}
            </template>
          </vxe-column>
          <vxe-column fixed="right" width="230" title="操作">
            <template v-slot="{ row }">
              <el-button type="text" size="big" @click.stop="memberDetail(row)">详情</el-button>
              <el-button type="text" size="big" @click.stop="editStatus(row)" v-if="row.status == '0'">禁用</el-button>
              <el-button type="text" size="big" @click.stop="editStatus(row)" v-if="row.status == '1'">启用</el-button>
              <el-button type="text" size="big" @click.stop="updateError(row)"
                v-if="row.isException == '1'">处理异常</el-button>
              <el-button type="text" size="big" style="margin-right: 10px; color: red"
                @click="$delFn('会员', del, row)">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :is-show-btn="isShowBtn" :show-dialog.sync="showDialog" :width="width"
      :title="title" @btnOk="btnOk" @close="close">
      <div style="margin-left: 8%">
        <el-row :gutter="24" style="margin-bottom: 40px">
          <el-col :span="12">注册门店：{{
            memberInfo.storeName ? memberInfo.storeName : ""
          }}</el-col>
          <el-col :span="12">核销次数：{{
            memberInfo.exchangeCount ? memberInfo.exchangeCount : 0
          }}</el-col>
        </el-row>
        <el-row :gutter="24" style="margin-bottom: 40px">
          <el-col :span="12">会员名称：{{
            memberInfo.name ? memberInfo.name : memberInfo.phone
          }}</el-col>
          <el-col :span="12">手机号：{{ memberInfo.phone ? memberInfo.phone : "" }}</el-col>
        </el-row>
        <el-row :gutter="24" style="margin-bottom: 40px">
          <el-col :span="12">累计消费金额：{{
            memberInfo.cumulativeMoney ? memberInfo.cumulativeMoney : 0
          }}</el-col>
          <el-col :span="12">累计发放{{ $tokenName }}：{{
            memberInfo.cumulativeToken ? memberInfo.cumulativeToken : 0
          }}</el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">现有{{ $tokenName }}数量：{{
            memberInfo.tokenNum ? memberInfo.tokenNum : 0
          }}</el-col>
          <el-col :span="12">已作废{{ $tokenName }}：{{
            memberInfo.voidToken ? memberInfo.voidToken : 0
          }}</el-col>
        </el-row>
      </div>
    </AddDialog>
  </div>
</template>

<script>
import {
  getMemberList,
  getShopEasy,
  delMember,
  getMembeDetail,
  relieveAbnormal,
  disableMember
} from "@/api/shop";
import AddDialog from "@/components/Goods/AddDialog";
import { parseTime2 } from "@/utils";
export default {
  name: "MemberInfo",
  filters: {
    parseTime(time) {
      return parseTime2(time);
    },
  },
  components: {
    AddDialog,
  },
  data() {
    return {
      addtions: {
        names: "",
      },
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      name: "",
      shopId: "",
      sort: '',
      isException: '',
      shopList: [],
      memberList: [],
      showDialog: false,
      title: "会员详情",
      width: "40%",
      isSearch: false,
      isShowBtn: true,
      memberInfo: {
        storeName: "",
        exchangeNum: "",
        name: "",
        phone: "",
        tokenNum: "",
        exchangeCount: "",
        voidToken: "",
        cumulativeToken: "",
        cumulativeMoney: "",
      },
    };
  },
  created() {
    this.getMemberList();
    this.getShopEasy();
  },
  methods: {
    // 获取会员列表
    async getMemberList() {
      if (!this.isSearch) {
        this.name = "";
        this.shopId = "";
        this.isException = "";
      }
      const res = await getMemberList({
        ...this.page,
        phone: this.name,
        storeId: this.shopId,
        isException: this.isException,
        filter: this.sort
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.memberList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("memberList", this.memberList);
        const $table = this.$refs.xTable1
        $table.loadData(this.memberList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getShopEasy() {
      const res = await getShopEasy();
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log(result);
        this.shopList = result.store;
        console.log("shopList", this.shopList);
      } else {
        // this.$message.error(res.message);
      }
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getMemberList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getMemberList();
    },
    async memberDetail(row) {
      // console.log(row)
      this.showDialog = true;
      const res = await getMembeDetail({ id: row.id });
      if (res.code === 200) {
        const result = res.data;
        console.log(result);
        this.memberInfo.storeName = result.storeName;
        this.memberInfo.exchangeNum = result.exchangeNum;
        this.memberInfo.name = result.name;
        this.memberInfo.phone = result.phone;
        this.memberInfo.tokenNum = result.tokenNum;
        this.memberInfo.exchangeCount = result.exchangeCount;
        this.memberInfo.voidToken = result.voidToken;
        this.memberInfo.cumulativeToken = result.cumulativeToken;
        this.memberInfo.cumulativeMoney = result.cumulativeMoney;
        // console.log('memberInfo', this.memberInfo)
      }
      // this.memberInfo = row
      // console.log(this.memberInfo)
    },
    async del(row) {
      console.log(111);
      console.log("删除", row);
      const res = await delMember({ id: row.id });
      console.log(res);
      if (res.code === 200) {
        // 判断当前数据是不是当页最后一条，如果是页码减1
        this.$message.success("删除成功");
        if (this.memberList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getBrand();
      } else {
        // this.$message.error(res.message);
      }
    },
    btnOk() {
      this.showDialog = false;
      this.memberInfo = {
        storeName: "",
        exchangeNum: "",
        name: "",
        phone: "",
        tokenNum: "",
        exchangeCount: "",
        voidToken: "",
        cumulativeToken: "",
        cumulativeMoney: "",
      };
    },
    close() {
      this.showDialog = false;
      this.memberInfo = {
        storeName: "",
        exchangeNum: "",
        name: "",
        phone: "",
        tokenNum: "",
        exchangeCount: "",
        voidToken: "",
        cumulativeToken: "",
        cumulativeMoney: "",
      };
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };

      this.isSearch = true;
      this.getMemberList()
    },
    resetFn() {
      this.name = "";
      this.shopId = "";
      this.isSearch = false;
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.memberList = [];
      this.getMemberList();
    },
    sortFn(val) {
      this.sort = val
      this.getMemberList()
    },
    editStatus(val) {
      this.$confirm(`确认修改用户状态吗, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log('11111');
          const res = await disableMember({ id: val.id });
          if (res.code === 200) {
            this.$message.success(res.message);
            this.getMemberList();
          }
        })
        .catch(() => { });
    },
    updateError(val) {
      this.$confirm(`确认处理该用户异常状态吗, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log('11111');
          const res = await relieveAbnormal({ id: val.id });
          if (res.code === 200) {
            this.$message.success(res.message);
            this.getMemberList();
          }
        })
        .catch(() => { });
    }
  },
};
</script>

<style lang="scss" scoped>
.iconCls:hover {
  color: #ee2b08 !important;
}

.iconClsActived {
  color: #ee2b08 !important;
}
</style>
